package com.wydstation.power.properties.repository;

import com.wydstation.power.properties.dto.device.DeviceInfoDTO;
import com.wydstation.power.properties.util.UniqueIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.reactive.TransactionalOperator;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Optional;

@Repository
public class DeviceInfoRepository {

    @Autowired
    private DatabaseClient databaseClient;


    @Transactional
    public Flux<Void> insertDeviceInfo(List<DeviceInfoDTO> deviceInfoList) throws Exception {
        Flux<Void> empty = Flux.empty();
        for (int i = 0; i < deviceInfoList.size(); i++) {
            DeviceInfoDTO deviceInfoDTO = deviceInfoList.get(i);
            Optional<DeviceInfoDTO.DeviceInfo> deviceInfo = Optional.ofNullable(deviceInfoDTO.getDeviceInfo());
            if (deviceInfo.isPresent()) {
                empty.mergeWith(databaseClient.insert().into(DeviceInfoDTO.DeviceInfo.class).using(deviceInfoDTO.getDeviceInfo()).then());
            }
            Optional<DeviceInfoDTO.DeviceInfoBoxInvert> deviceInfoBoxInvert = Optional.ofNullable(deviceInfoDTO.getDeviceInfoBoxInvert());
            if (deviceInfoBoxInvert.isPresent()) {
                empty.mergeWith(databaseClient.insert().into(DeviceInfoDTO.DeviceInfoBoxInvert.class).using(deviceInfoDTO.getDeviceInfoBoxInvert()).then());
            }
            //一个设备多个支路
            Optional<List<DeviceInfoDTO.DeviceInfoBranch>> deviceInfoBranch = Optional.ofNullable(deviceInfoDTO.getDeviceInfoBranchList());
            if (deviceInfoBranch.isPresent()) {
                Mono.fromCallable(() -> deviceInfoBranch.get()).map(deviceInfoBranches1 -> {
                    return deviceInfoBranches1.stream().map(branch -> databaseClient.insert().into(DeviceInfoDTO.DeviceInfoBranch.class).using(branch).then().block());
                }).block();
            }
            Optional<DeviceInfoDTO.DeviceInfoFan> deviceInfoFan = Optional.ofNullable(deviceInfoDTO.getDeviceInfoFan());
            if (deviceInfoFan.isPresent()) {
                empty.mergeWith(databaseClient.insert().into(DeviceInfoDTO.DeviceInfoFan.class).using(deviceInfoDTO.getDeviceInfoFan()).then());
            }
            Optional<DeviceInfoDTO.DeviceInfoMatrix> getDeviceInfoMatrix = Optional.ofNullable(deviceInfoDTO.getDeviceInfoMatrix());
            if (getDeviceInfoMatrix.isPresent()) {
                empty.mergeWith(databaseClient.insert().into(DeviceInfoDTO.DeviceInfoMatrix.class).using(deviceInfoDTO.getDeviceInfoMatrix()).then());
            }
        }
        empty.then().block();
        return empty;
    }

}
